<template>
  <div id="userLoginPage">
    <div class="userLogin">
      <h2 class="title">账号登录</h2>
      <div class="desc">智能云图库管理与协作平台</div>
      <a-form
        :model="formState"
        autocomplete="off"
        @finish="handleSubmit"
      >
        <a-form-item
          name="userAccount"
          :rules="[{ required: true, message: '请输入用户名！' }]"
        >
          <a-input v-model:value="formState.userAccount" placeholder="请输入账号"/>
        </a-form-item>

        <a-form-item
          name="userPassword"
          :rules="[{ required: true, message: '请输入密码！' }]"
        >
          <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
        </a-form-item>
        <div class="tips">
          没有账号？
          <RouterLink to="/user/register">去注册</RouterLink>
        </div>
        <a-form-item>
          <a-button type="primary" html-type="submit" style="width: 100%">登录</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { loginUsingPost } from '@/apis/UserInterface.ts'

const router = useRouter()
const loginUserStore = useLoginUserStore()

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})
const handleSubmit = async (values: any) => {
  const res = await loginUsingPost(values)
  // 登录成功，把登录态保存到全局状态中
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')
    router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error('登录失败，' + res.data.message)
  }
}
</script>

<style>
#userLoginPage{
  height: 100vh;
  padding-top: 30px;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}

.userLogin {
  display: block;
  max-width: 360px;
  border-radius: 15px;
  border: 0;
  margin: 64px auto;
  outline: none;
  background: rgba(255, 255, 255, 0.9);
  padding: 30px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  text-align: center;
}

/*.userLogin:hover {
  !* 盒子阴影 *!
  box-shadow: 4px 20px 40px 5px rgba(0, 0, 0, .1);
  !* 盒子向上移动效果，改变上外边距 *!
  margin-top: 8px;
}*/

.title {
  text-align: center;
  font-size: 45px;
  color: #fefefe;
  text-shadow: 0 0 0.5em #0ae642, 0 0 0.2em #5c5c5c;
  margin-bottom: 32px;
}

.desc {
  text-align: center;
  font-size: 25px;
  color: #959595;
  text-shadow: 1px 1px 3px red;
  margin-bottom: 32px;
}

.tips {
  margin-bottom: 16px;
  color: #bbb;
  font-size: 13px;
  text-align: right;
}

</style>
